﻿function Enum() {}

//
Enum.state = {
  not_init:       0,
  init_null:      1,
  list_wb:        2,
  list_wb_my:     3,
  list_wb_at_me:  4,
  list_dm:        5,
  list_friend:    6,
  list_followers: 7,
  list_unread:    8,
  list_comments:  9,
  list_fav:       10,
  total:          11
}

//
Enum.error = {
  nil:            0,
  cmd:            1,
  param:          2,
  server:         3,
  state:          4,
  total:          5
}

//
Enum.err_str = {
  nil:            '',
  cmd:            'Not recognized  command',
  param:          'Wrong param',
  server:         'Server error',
  state:          'State error',
  total:          ''
}

//
Enum.cmd  = [
  {name:'wb',   aliases:["weibo"],          parameters: [],                 description:"get Weibo messsages, eg: wb"},
  {name:'my',   aliases:["my_wb"],          parameters: [],                 description:"show Weibo messages post by you, eg: my"},
  {name:'@me',  aliases:["at_me, fwds"],    parameters: [],                 description:"show Weibo messages you are mentioned, eg: @me"},
  {name:'m',    aliases:["more"],           parameters: [],                 description:"get more results, eg: m"},
  {name:'fwd',  aliases:["forward"],        parameters: ["&#60;id&#62; [string]"],  description:"forward this message, eg: fwd 1 <your msg>"},
  {name:'cmt',  aliases:["comment"],        parameters: ["&#60;id&#62; [string]"],  description:"comment this message, eg: cmt 1 <your msg>"},
  {name:'del',  aliases:["delete"],         parameters: ["&#60;id&#62;"],   description:"delete weibo, eg: del 1"},
  {name:'up',   aliases:["update", "ci"],   parameters: ["[string]"],       description:"write a new Weibo, update status, eg: up <your msg>"},
  {name:'un',   aliases:["unread"],         parameters: [""],               description:"get unread num (comment, follower, new status, dm, mention), eg: un"},
  {name:'cmts', aliases:["comments"],       parameters: [""],               description:"get comments list, eg: cmts"},
  {name:'sp',   aliases:["show_pic"],       parameters: ["&#60;id&#62;"],   description:"show the specified messasge's middle picture, eg: sp 1"},
  {name:'spb',  aliases:["show_pic_big"],   parameters: ["&#60;id&#62;"],   description:"show the specified messasge's big picture, eg: spb 1"},
  {name:'pic',  aliases:["img"],            parameters: ["on", "off"],      description:"whether show thumb image in weibo list, eg: pic off"},
  {name:'cl',   aliases:["clear"],          parameters: [],                 description:"clear the screen, eg: cl"},
  {name:'h',    aliases:["help", "?"],      parameters: [],                 description:"get help info, list commands, eg: h"},
  {name:'exit', aliases:["logout", "quit"], parameters: [],                 description:"log out of wbsh, close this tab, eg: exit"},
  {name:'flw',  aliases:["followers"],      parameters: [],                 description:"get followers list, eg: flw"},
  {name:'dm',   aliases:["direct_msg"],     parameters: [],                 description:"get direct msg list, eg: dm"},
];

//
Enum.cmd_index  = {
  weibo:        0,
  my:           1,
  at_me:        2,
  more:         3,
  forward:      4,
  comment:      5,
  del:          6,
  update:       7,
  unread:       8,
  comments:     9,
  show_pic:     10,
  show_pic_big: 11,
  pic_on_off:   12,
  clear:        13,
  help:         14,
  logout:       15,
  followers:    16,
  dm:           17,
}

//
var curr_cmd_str = new Array();
var curr_state   = Enum.state.not_init;
var curr_page    = 1;
var curr_count   = 5;
var curr_error   = Enum.error.nil;
var curr_err_str = '';
var curr_ids     = new Array();
var curr_imgs    = new Array();
var curr_imgs_big= new Array();
var curr_user_id = '';
var curr_img_show = true;

// user unread numbers
var curr_un_comments_num  = 0;
var curr_un_followers_num = 0;
var curr_un_dm_num        = 0;
var curr_un_mentions      = 0;

//
function json_error_handle(json) {
  // error handling
  if (json.error || json.error_code) {
    curr_error = Enum.error.server;
    curr_err_str = json.error;
    output_append(curr_err_str);
    return true;
  }
  return false;
}
// handle weibo json, show result
function json_weibo_callback(json) {
  if (json_error_handle(json)) {
    return;
  }
  
  // no error happened
  switch (curr_state) {
    case Enum.state.list_wb:
    case Enum.state.list_wb_my:
    case Enum.state.list_wb_at_me:
      // clean first, then show more result
      $("#output").html('');
      output_wb(json);
      break;
    default:
      curr_error = Enum.error.state;
      curr_err_str = convert_err_type_to_str(curr_error);
      output_append(curr_err_str);
      break;
  }
  
  scroll_down_and_enable_input();
}

// handle action json
function json_action_callback(json) {
  if (json_error_handle(json)) {
    return;
  }
  
  var html = json.action + ' done';
  output_append(html);
  
  if (json.action == Enum.cmd[Enum.cmd_index.logout].name) {
    // if it's logout, reload the page
    window.location.reload();
  }

  scroll_down_and_enable_input();
}

// handle unread json
function json_unread_callback(json) {
  if (json_error_handle(json)) {
    return;
  }
      
  var html = '';
  curr_un_comments_num = json.comments;
  curr_un_followers_num = json.followers;
  curr_un_dm_num = json.dm;
  curr_un_mentions = json.mentions;
  html += 'News, Comments:<a href="http://weibo.com/comments" target="_blank">' + json.comments + "</a>, ";
  html += 'Followers:<a href="http://weibo.com/'+curr_user_id+'/fans" target="_blank">' + json.followers + "</a>, ";
  html += 'DM:<a href="http://weibo.com/messages" target="_blank">' + json.dm + "</a>, ";
  html += '@Me:<a href="http://weibo.com/atme" target="_blank">' + json.mentions + "</a>, "; //json.new_status

  output_append(html);
  scroll_down_and_enable_input();
}

// comments list
function json_comments_callback(json) {
  if (json_error_handle(json)) {
    return;
  }
  
  // clean first, then show more result
  $("#output").html('');
  
  var html = '<table border="0"><tbody>';
  for (var i = 1; i <= json.length; ++i) {
    var item = json[i-1];
    var verified = '';//item.user.verified ? '[V]'
    var url = 'http://weibo.com/'+item.user.id;
    var content = item.text;
    var orginal_cmt = '';
    if (item.reply_comment) {
      orginal_cmt += '<span class="quote">Reply to ';
      orginal_cmt += '<a href="http://weibo.com/'+item.reply_comment.user.id+'" target="_blank">';
      orginal_cmt += '@'+item.reply_comment.user.name+'</a>';
      orginal_cmt += ':'+item.reply_comment.text+'</span>';
    } else {
      orginal_cmt += '<span class="quote">Reply to my msg:'+item.status.text+'</span>';
    }

    html += '<tr><td valign="top" class="less">'+ i +')&nbsp;</td><td>';
    html += '<a href="' + url + '" target="_blank">' + item.user.name + verified + '</a>：' + content + '<span class="date">('+ item.created_at +')</span><br>';
    html += orginal_cmt;
    html += '<br/>&nbsp;</td></tr>';
  }

  output_append(html);
  scroll_down_and_enable_input();
}

// followers callback
function json_followers_callback(json) {
  if (json_error_handle(json)) {
    return;
  }

  // clean first, then show more result
  $("#output").html('');

  var html = '<table border="0"><tbody>';
  if (curr_page > 1) {
    json = json.users;
  }
  for (var i = 1; i <= json.length; ++i) {
    var item = json[i-1];
    var verified = item.verified ? '[V]' : '';
    var url = 'http://weibo.com/'+item.id;

    html += '<tr><td valign="top" class="less">'+ i +')&nbsp;</td><td>';
    html += '<a href="' + url + '" target="_blank">' + item.name + verified + '</a> ('+ item.gender +') ' + item.location + '<br/>';
    html += '<span class="quote">Follow: ' + item.friends_count + ', Follower: ' + item.followers_count + ', Favorite: '+item.favourites_count+', Weibo:' + item.statuses_count;
    if (!item.following) {
      //html += '(Not following)';
    }
    html += '<br/>';
    html += 'Description: ' + item.description + '<br/>';
    html += 'Latest Weibo: ' + item.status.text + '<span class="date">('+ item.status.created_at +')</span>';
    html += '</span><br>&nbsp;</td></tr>';
  }

  output_append(html);
  scroll_down_and_enable_input();
}

// parse weibo
function parse_cmd_weibo(cmd_index) {
  if (curr_cmd_str.length != 1 && curr_cmd_str[1] != '') {
    // error param
    curr_error = Enum.error.param;
    curr_err_str = convert_err_type_to_str(curr_error);
    return false;
  }

  var orig_curr_page = curr_page;
  curr_page = 1;
  if (cmd_index == Enum.cmd_index.weibo) {  
    // friends_timeline
    curr_state = Enum.state.list_wb;
    $.getJSON("weibo.php", {type: 'friends',  id: curr_user_id, page: curr_page, count: curr_count}, json_weibo_callback);
  } else if (cmd_index == Enum.cmd_index.my) {
    // user_timeline
    curr_state = Enum.state.list_wb_my;
    $.getJSON("weibo.php", {type: 'user',     id: curr_user_id, page: curr_page, count: curr_count}, json_weibo_callback);
  } else if (cmd_index == Enum.cmd_index.at_me) {
    curr_state = Enum.state.list_wb_at_me;
    $.getJSON("weibo.php", {type: 'mentions', id: curr_user_id, page: curr_page, count: curr_count}, json_weibo_callback);
  } else {
    curr_page = orig_curr_page;
    curr_error = Enum.error.state;
    curr_err_str = convert_err_type_to_str(curr_error);
    return false;
  }
  return true;
}

// comments list
function parse_cmd_comments() {
  if (curr_cmd_str.length != 1 && curr_cmd_str[1] != '') {
    // error param
    curr_error = Enum.error.param;
    curr_err_str = convert_err_type_to_str(curr_error);
    return false;
  }
  
  curr_page = 1;
  curr_state = Enum.state.list_comments;
  $.getJSON("comments.php", {page: 1, count: curr_count}, json_comments_callback);
  return true;
}

// followers list
function parse_cmd_followers() {
  if (curr_cmd_str.length != 1 && curr_cmd_str[1] != '') {
    // error param
    curr_error = Enum.error.param;
    curr_err_str = convert_err_type_to_str(curr_error);
    return false;
  }
  
  curr_page = 1;
  curr_state = Enum.state.list_followers;
  $.getJSON("followers.php", {id: curr_user_id, page: curr_page, count: curr_count}, json_followers_callback);
  return true;
}

// parse more
function parse_cmd_more() {
  if (curr_state < Enum.state.list_wb || curr_state > Enum.state.list_fav) {
    curr_error = Enum.error.cmd;
    curr_err_str = convert_err_type_to_str(curr_error);
    return false;
  }

  curr_page += 1;
  switch (curr_state) {
    case Enum.state.list_wb:
      $.getJSON("weibo.php", {type: 'friends',  id: curr_user_id, page: curr_page, count: curr_count}, json_weibo_callback);
      break;
    case Enum.state.list_wb_my:
      $.getJSON("weibo.php", {type: 'user',     id: curr_user_id, page: curr_page, count: curr_count}, json_weibo_callback);
      break;
    case Enum.state.list_wb_at_me:
      $.getJSON("weibo.php", {type: 'mentions', id: curr_user_id, page: curr_page, count: curr_count}, json_weibo_callback);
      break;
    case Enum.state.list_comments:
      $.getJSON("comments.php", {page: curr_page, count: curr_count}, json_comments_callback);
      break;
    case Enum.state.list_followers:
      $.getJSON("followers.php", {id: curr_user_id, page: curr_page, count: curr_count}, json_followers_callback);
      break;
    default:
      curr_error = Enum.error.state;
      curr_err_str = convert_err_type_to_str(curr_error);
      return false;
  }
  return true;
}

// parse pic_on_off
function parse_cmd_pic_on_off() {
  if (curr_cmd_str[1] == 'on') {
    curr_img_show = true;
    return true;
  } else if (curr_cmd_str[1] == 'off') {
    curr_img_show = false;
    return true;
  } else {
    curr_error = Enum.error.param;
    curr_err_str = convert_err_type_to_str(curr_error);
  }
  return false;
}

// parse clear
function parse_cmd_clear() {
  $("#output").html('');
  scroll_down_and_enable_input();
  return true;
}

// parse help
function parse_cmd_help() {
  var html = '';
  html += '<table border="0" class="help"><tbody>';
  html += '<tr><td class="less">command</td><td class="less">aliases</td><td class="less">parameters</td><td class="less">description</td></tr>';
  
  for (var i = 0; i < Enum.cmd.length; ++i) {
    html += '<tr><td class="info">' + Enum.cmd[i].name + '</td>';
    var aliases_str = '';
    if (Enum.cmd[i].aliases.length > 0) {
      aliases_str += '(';
      aliases_str += Enum.cmd[i].aliases[0];
      for (var j = 1; j < Enum.cmd[i].aliases.length; ++j) {
        aliases_str += ',' + Enum.cmd[i].aliases[j];
      }
      aliases_str += ')';
    }
    html += '<td>' + aliases_str + '</td>';
    html += '<td>' + Enum.cmd[i].parameters + '</td>';
    html += '<td>' + Enum.cmd[i].description + '</td></tr>';
  }
  html += '</tbody></table><br>';
  output_append(html);
  scroll_down_and_enable_input();
  return true;
}

// picType: 0 small, 1 middle, 2 large(orignal)
function parse_cmd_show_pic(picType) {
  var msgid_index = parseInt(curr_cmd_str[1]);
  
  curr_error = Enum.error.nil;
  if (curr_cmd_str.length < 2 || msgid_index > curr_ids.length ||
      (picType == 1 && curr_imgs[msgid_index-1] == '') ||
      (picType == 2 && curr_imgs_big[msgid_index-1] == '')) {
    // error param
    curr_error = Enum.error.param;
    curr_err_str = convert_err_type_to_str(curr_error);
  }

  if (curr_state != Enum.state.list_wb && curr_state != Enum.state.list_wb_my) {
    // state MUST be wb list
    curr_error = Enum.error.state;
    curr_err_str = convert_err_type_to_str(curr_error);
  }
  
  if (curr_error != Enum.error.nil) {
    output_append(curr_err_str);
    return;
  }

  // image exists
  if (picType == 1) {
    $('#'+curr_ids[msgid_index-1]).attr("src",curr_imgs[msgid_index-1]);
  } else if (picType == 2) {
    $('#'+curr_ids[msgid_index-1]).attr("src",curr_imgs_big[msgid_index-1]);
  }
}


// unread numbers 
function parse_cmd_unread() {
  $.getJSON("unread.php", {with_new_status: 1}, json_unread_callback);
  return true;
}

// parse forward/comment/logout
function parse_cmd_action(cmdIndex) {
  switch (cmdIndex) {
    case Enum.cmd_index.logout:
      $.getJSON("action.php", {action: Enum.cmd[cmdIndex].name}, json_action_callback);
      return true;
    case Enum.cmd_index.forward:
    case Enum.cmd_index.comment:
    case Enum.cmd_index.update:
    case Enum.cmd_index.del:
      var msgid_index = parseInt(curr_cmd_str[1]);
      var post_text  = '';
      if (((cmdIndex == Enum.cmd_index.comment || cmdIndex == Enum.cmd_index.forward) && curr_cmd_str.length < 3) ||
          curr_cmd_str.length < 2 || curr_cmd_str[1] == '' || msgid_index > curr_ids.length) {
        // error param
        curr_error = Enum.error.param;
        curr_err_str = convert_err_type_to_str(curr_error);
        return false;
      }
      if (cmdIndex != Enum.cmd_index.update && // other case it must be in list state
          curr_state != Enum.state.list_wb && curr_state != Enum.state.list_wb_my) {
        curr_error = Enum.error.state;
        curr_err_str = convert_err_type_to_str(curr_error);
      }
  
      if (cmdIndex == Enum.cmd_index.comment) {
        post_text  = curr_cmd_str[2];
        $.getJSON("action.php", {action: Enum.cmd[cmdIndex].name, msgid: curr_ids[msgid_index-1], text: post_text}, json_action_callback);
      } else if (cmdIndex == Enum.cmd_index.forward) {
        post_text  = curr_cmd_str[2];
        $.getJSON("action.php", {action: Enum.cmd[cmdIndex].name, msgid: curr_ids[msgid_index-1], text: post_text}, json_action_callback);
      } else if (cmdIndex == Enum.cmd_index.update) {
        post_text  = curr_cmd_str[1];
        $.getJSON("action.php", {action: Enum.cmd[cmdIndex].name, text: post_text}, json_action_callback);
      } else if (cmdIndex == Enum.cmd_index.del) {
        $.getJSON("action.php", {action: Enum.cmd[cmdIndex].name, msgid: curr_ids[msgid_index-1]}, json_action_callback);
      }
      return true;
    default:
      curr_error = Enum.error.cmd;
      curr_err_str = convert_err_type_to_str(curr_error);
      break;
  }
  
  return false;
}

// compare a string to a cmd string (including aliases)
function compare_to_cmd_string(str, cmdStr) {
  if (str == cmdStr) {
    return true;
  }

  for (var i = 0; i < Enum.cmd.length; ++i) {
    if (Enum.cmd[i].name == cmdStr) {
      for (var j = 0; j < Enum.cmd[i].aliases.length; ++j) {
        // loop aliases
        if (Enum.cmd[i].aliases[j] == str) {
          return true;
        }
      }
    }
  }
  // not match
  return false;
}

// convert cmd str to index
function get_cmd_index(str) {
  for (var i = 0; i < Enum.cmd.length; ++i) {
    // match name
    if (Enum.cmd[i].name == str) {
      return i;
    }

    for (var j = 0; j < Enum.cmd[i].aliases.length; ++j) {
      // loop aliases
      if (Enum.cmd[i].aliases[j] == str) {
        // match aliase
        return i;
      }
    }
  }
  // not found
  return -1;
}

// parse cmd main function
function parse_cmd() {
  if (curr_state <= Enum.state.not_init) {
    return;
  }

  var ret = false;

  // handle string array {curr_command}
  var cmdIndex = get_cmd_index(curr_cmd_str[0]);
  switch (cmdIndex) {
    // handle local
    case Enum.cmd_index.pic_on_off:
      parse_cmd_pic_on_off();
      return;
    case Enum.cmd_index.clear:
      parse_cmd_clear();
      return;
    case Enum.cmd_index.help:
      parse_cmd_help();
      return;
    case Enum.cmd_index.show_pic:
      parse_cmd_show_pic(1);
      return;
    case Enum.cmd_index.show_pic_big:
      parse_cmd_show_pic(2);
      return;

    // BELLOW: async call, need server to handle
    case Enum.cmd_index.weibo:
    case Enum.cmd_index.my:
    case Enum.cmd_index.at_me:
      ret = parse_cmd_weibo(cmdIndex);
      break;
    case Enum.cmd_index.more:
      ret = parse_cmd_more();
      break;
    case Enum.cmd_index.unread:
      ret = parse_cmd_unread();
      break;
    case Enum.cmd_index.comments:
      ret = parse_cmd_comments();
      break;
    case Enum.cmd_index.followers:
      ret = parse_cmd_followers();
      break;
    default:
      ret = parse_cmd_action(cmdIndex);
      break;
  }

  if (ret) {
    disable_input(true);
  } else {
    output_append(curr_err_str);
  }
}

//
function convert_err_type_to_str(t) {
  switch(t) {
    case Enum.error.cmd:
      return Enum.err_str.cmd;
    case Enum.error.param:
      return Enum.err_str.param;
    case Enum.error.server:
      return curr_err_str;
  }
  return Enum.err_str.nil;
}

//
function parse_cmd_str_to_array(str) {
  var s = $.trim(str);
  curr_cmd_str = s.split(' ');  
  var cmdIndex = get_cmd_index(curr_cmd_str[0]);
  if (cmdIndex == Enum.cmd_index.forward ||
      cmdIndex == Enum.cmd_index.comment ||
      cmdIndex == Enum.cmd_index.update) {
    // these 3 cases, need specail handle
    curr_cmd_str = [];  // clear it
    var reMatch;
    if (cmdIndex == Enum.cmd_index.update) {
      reMatch = /^\s*([a-zA-Z]+)\s+(.*)$/;
    } else {
      reMatch = /^\s*([a-zA-Z]+)\s+([0-9]+)\s*(.*)$/;
    }
    if (str.match(reMatch)) {
      curr_cmd_str[0] = RegExp.$1;
      curr_cmd_str[1] = RegExp.$2;
      if (cmdIndex != Enum.cmd_index.update) {
        curr_cmd_str[2] = RegExp.$3;
      }
      if (cmdIndex == Enum.cmd_index.forward) { // append original text
        curr_cmd_str[2] += '//@' + $('#WbText' + curr_cmd_str[1]).text();
        if (curr_cmd_str[2].length >= 140) {
          curr_cmd_str[2] = curr_cmd_str[2].substring(0,140);
        }
      }
    }
  }
}

//
function page_loaded() {
  $("#inputfield").focus();
  $.getJSON("user.php", function(json) {
    if (json.error || json.error_code) {
      output_append(json.error);
    } else {
      curr_state = Enum.state.init_null;
      curr_user_id = json.id;
      $("#prompt").html(json.name + "@weibo.com&gt;&nbsp;");
    }
  });
}

//
function output_append(s) {
  $("<div/>").html(s).appendTo("#output");
}


//
function output_wb(json) {
  var html = '<table border="0"><tbody>';

  for (var i = 1; i <= json.length; ++i) {
    var wb_index = (((curr_page - 1) * curr_count) + i);
    var item = json[i-1];
    var verified = '';//item.user.verified ? '[V]'
    var url = 'http://weibo.com/'+item.user.id;
    var content = item.text;
    var quote = '';
    if (item.retweeted_status) {
      quote += '<span class="quote">';
      quote += '<a href="http://weibo.com/'+item.retweeted_status.user.id+'" target="_blank">';
      quote += '@'+item.retweeted_status.user.name+'</a>';
      quote += ':'+item.retweeted_status.text+'<br></span>';
    }

    var imageHtml = '';
    var imageUrl = '';
    if (item.thumbnail_pic) {
      imageUrl = item.thumbnail_pic;
    } else if (item.retweeted_status && item.retweeted_status.thumbnail_pic) {
      imageUrl = item.retweeted_status.thumbnail_pic;
    }

    if (curr_img_show && imageUrl != '') {
      imageHtml = '<image id="'+ item.id +'" src="' + imageUrl+ '"/><br>';
    } else {
      imageHtml = '<image id="'+ item.id +'"/><br>';
    }

    html += '<tr><td valign="top" class="less">'+ wb_index +')&nbsp;</td><td>';
    html += '<span id="WbText'+wb_index+'"><a href="' + url + '" target="_blank">' + item.user.name + verified + '</a>：' + content + '</span><br>';
    html += quote + imageHtml;
    html += '<span class="date">'+ item.created_at +' Fwd:('+item.rt+') Comments:('+item.comments+')</span><br>&nbsp;</td></tr>';

    curr_ids[wb_index - 1] = item.id;
    if (item.bmiddle_pic) {
      curr_imgs[wb_index - 1]     = item.bmiddle_pic;
      curr_imgs_big[wb_index - 1] = item.original_pic;
    } else if (item.retweeted_status && item.retweeted_status.bmiddle_pic) {
      curr_imgs[wb_index - 1]     = item.retweeted_status.bmiddle_pic;
      curr_imgs_big[wb_index - 1] = item.retweeted_status.original_pic;
    } else {
      curr_imgs[wb_index - 1]     = '';
      curr_imgs_big[wb_index - 1] = '';
    }
  }

  html += '</tbody></table>';
  output_append(html);
}

//
function disable_input(disable) {
  if (disable) {
    $('#input').hide();
  } else {
    $('#input').show();
  }
}

//
function scoll_down() {
  $('html, body').animate({scrollTop: $(document).height()});
}

//
function clear_input() {
  $('#inputfield').val('');
}

//
function scroll_down_and_enable_input() {
  scoll_down();
  disable_input(false);
}

//
$(function() {
  $(document).bind("dblclick", (function () {
  }));

  $(document).bind('click', function() {
    $('input').each(function() {
      if (this.createTextRange) {
        var r= this.createTextRange(); 
        r.collapse();
        r.select();
      }
      $(this).focus();
    });
  });
    
  $(document).bind('keydown', function(evt) {
    if (evt.keyCode == 13) {
      var cmdStr = $('#inputfield').val();
      output_append('<div class="input"><span class="less">'+$("#prompt").html()+'</span>'+cmdStr+'</div>');
      if (cmdStr.length == 0) {
        return;
      }
      parse_cmd_str_to_array(cmdStr);
      clear_input();
      parse_cmd();
    }
  });
});
